package com.bjgzy.rating.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bjgzy.rating.R;
import com.bjgzy.rating.R2;
import com.bjgzy.rating.di.component.DaggerRatingDetailComponent;
import com.bjgzy.rating.mvp.contract.RatingDetailContract;
import com.bjgzy.rating.mvp.presenter.RatingDetailPresenter;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.RatingDetailData;
import com.eduhzy.ttw.commonsdk.entity.RatingHomeData;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.Subscriber;

import java.text.SimpleDateFormat;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-8-7 16:51:51
 */
@Route(path = RouterHub.RATING_RATINGDETAILFRAGMENT)
public class RatingDetailFragment extends BaseFragment<RatingDetailPresenter> implements RatingDetailContract.View {
    RatingHomeData mData;
    @BindView(R2.id.iv_logo)
    ImageView ivLogo;
    @BindView(R2.id.tv_host)
    TextView tvHost;
    @BindView(R2.id.tv_time)
    TextView tvTime;
    @BindView(R2.id.tv_upload_time)
    TextView tvUploadTime;
    @BindView(R2.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R2.id.tv_content)
    TextView tvContent;
    @BindView(R2.id.tv_status)
    TextView tvStatus;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    public static RatingDetailFragment newInstance(RatingHomeData data) {

        RatingDetailFragment fragment = new RatingDetailFragment();
        Bundle args = new Bundle();
        args.putParcelable(Constants.ROUTER_DATA, data);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerRatingDetailComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
        mData = getArguments().getParcelable(Constants.ROUTER_DATA);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.rating_fragment_detail, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        swipeLayout.setOnRefreshListener(() -> getResearchById());
        getResearchById();
    }


    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(false);
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    private void getResearchById() {
        mPresenter.getResearchById(mData.getId(), mData.getConfigId(), SPUtils.getInstance().getString(Constants.USERID));
    }

    @Subscriber(tag = RouterHub.RATING_RATINGDETAILFRAGMENT)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.WORKS_UPLOAD_SUCCESS:
                getResearchById();
                break;
            default:
        }
    }

    @Override
    public void update(RatingDetailData data) {
        if (ObjectUtils.isNotEmpty(data)) {
            tvStatus.setText(data.getStatusStr());
            if (data.getStatus() == 0 || data.getStatus() == 6 || data.getStatus() == 4) {
                tvStatus.setSelected(false);
            } else {
                tvStatus.setSelected(true);
            }
            Glide.with(this)
                    .load(data.getImgUrl())
                    .apply(RequestOptions.placeholderOf(R.drawable.public_img_default).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL))
                    .into(ivLogo);
            tvHost.setText(data.getGroupName());
            tvContent.setText(RxUtil.fromHtml(data.getDescribe()));
            String startDate = TimeUtils.millis2String(data.getStartDate(), new SimpleDateFormat("yyyy.MM.dd"));
            String endDate = TimeUtils.millis2String(data.getEndDate(), new SimpleDateFormat("yyyy.MM.dd"));
            tvTime.setText(startDate + "-" + endDate);
//            String publishStart = TimeUtils.millis2String(data.getPublicStartDate(), new SimpleDateFormat("yyyy.MM.dd"));
//            String publishEnd = TimeUtils.millis2String(data.getPublicEndDate(), new SimpleDateFormat("yyyy.MM.dd"));
//            tvUploadTime.setText(publishStart + "-" + publishEnd);
            String auditTime = TimeUtils.millis2String(data.getApplyStartDate(), new SimpleDateFormat("yyyy.MM.dd"));
            String auditEnd = TimeUtils.millis2String(data.getApplyEndDate(), new SimpleDateFormat("yyyy.MM.dd"));
            tvUploadTime.setText(auditTime + "-" + auditEnd);
        }
    }
}
